﻿using Microsoft.AspNetCore.Http;
using MiniFox.Web;
using MiniFox.Platform.File.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MiniFox.Platform.File.Web
{
    /// <summary>
    /// 
    /// </summary>
    public static class HttpFileExtensions
    {

        /// <summary>
        /// 
        /// </summary>
        /// <param name="httpRequest"></param>
        /// <returns></returns>
        public async static Task<FileData[]> HttpFileBatchLoadAsync(this HttpRequest httpRequest)
        {
            var array = await httpRequest.GetHttpFileStreamAsync();

            return array.FileBatchLoad();
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="httpRequest"></param>
        /// <returns></returns>
        public static FileData[] HttpFileBatchLoad(this HttpRequest httpRequest)
        {
            var array = httpRequest.GetHttpFileStream();

            return array.FileBatchLoad();

        }

        private static FileData[] FileBatchLoad(this IEnumerable<(string name, byte[] data, string fileName, string mimeType, string extName)> array)
        {
            var query = (from f in array
                         select new FileData
                         {
                             FileId = SequenceGUID.NewGUID(),
                             FormName = f.name,
                             FileName = f.fileName,
                             Data = f.data,
                             ContentType = f.mimeType,
                             CreateTime = DateTime.Now,
                             UpdateTime = DateTime.Now
                         });
            return query.ToArray();
        }
    }
}
